package com.hooya.fa.eu.api.util;

import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import static com.hooya.fa.eu.api.constant.FileNameConstant.EXCEL_FOLDER_NAME;

/**
 * @Description 1
 * @Author : Zongz
 * @Copyright Copyright (c) 豪雅集团
 * @Create 2024/1/2
 * @Version 1.0.0
 */
public class FileUtil {

    public static String getExcelPath() {
        ClassPathResource resource = new ClassPathResource(EXCEL_FOLDER_NAME);
        return resource.getPath();
    }

    public static File createNewFile(String pathName, String fileName) {
        File directory = new File(getExcelPath() + pathName);
        File file = new File(getExcelPath() + pathName + fileName);
//        if (!directory.exists() && !directory.mkdirs()) {
//            throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath());
//        }
        try {
            // 如果文件夹不存在，则创建它
            if (!directory.exists()) {
                directory.mkdirs();
                System.out.println("文件夹已成功创建: " + directory.getAbsolutePath());
            }
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    public static List<String> getFileNamesByPath(String pathName) {
        List<String> res = new LinkedList<>();
        File folder = new File(pathName);
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    res.add(file.getName());
                }
            }
        }
        return res;
    }
}
